var _ = require('lodash');
var Password = require("node-php-password");
var path = require("path");
var managersDAO = require(path.join(process.cwd(),"dao/ManagerDAO"));
var accountdao = require(path.join(process.cwd(),"dao/AccountDAO"));


// 获取所有权限
module.exports.modifyPassword = function(username,oldpwd,newpwd,cb) {
    console.log("密码");
    managersDAO.findOne({"mg_name":username},function(err,manager) {
        console.log(oldpwd);
        if(err || !manager) return cb("用户名不存在");
        if(Password.verify(oldpwd, manager.mg_pwd)){

            accountdao.update({"mg_id":manager.mg_id,"mg_pwd":Password.hash(newpwd)},function(err,manager){
                if(err) return cb("设置失败");
                cb(null,{
                    "id":manager.mg_id,
                    "rid":manager.role_id,
                    "username":manager.mg_name,
                    "mobile":manager.mg_mobile,
                    "email":manager.mg_email,
                    "mg_state":manager.mg_state ? 1 : 0
                });
            })
        } else {
            return cb("旧密码错误");
        }
    });



}
